# -*- coding: utf-8 -*-
"""
用户管理系统配置文件
@api-version: 1.0.0
"""

import os
from datetime import timedelta

class Config:
    """基础配置类"""
    
    # 基础配置
    SECRET_KEY = os.environ.get('SECRET_KEY') or 'dev-secret-key-change-in-production'
    
    # 数据库配置
    DB_HOST = '121.37.130.94'
    DB_PORT = 3306
    DB_NAME = 'snail_clouds_test'
    DB_USER = 'snail_clouds_test'
    DB_PASSWORD = 'snail_clouds_test'
    
    SQLALCHEMY_DATABASE_URI = f'mysql+pymysql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}?charset=utf8mb4'
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    SQLALCHEMY_ENGINE_OPTIONS = {
        'pool_size': 10,              # 连接池大小
        'pool_timeout': 20,           # 获取连接超时时间
        'pool_recycle': 3600,         # 1小时后回收连接，避免MySQL超时
        'pool_pre_ping': True,        # 连接前检查可用性
        'max_overflow': 5,            # 允许额外连接处理突发请求
        'connect_args': {
            'connect_timeout': 10,     # 建立连接超时时间
            'read_timeout': 30,        # 读取操作超时时间
            'write_timeout': 30        # 写入操作超时时间
        }
    }
    
    # JWT配置
    JWT_SECRET_KEY = os.environ.get('JWT_SECRET_KEY') or 'jwt-secret-string'
    JWT_ACCESS_TOKEN_EXPIRES = timedelta(hours=24)
    JWT_REFRESH_TOKEN_EXPIRES = timedelta(days=7)
    JWT_ALGORITHM = 'HS256'
    
    # CORS配置
    CORS_ORIGINS = ['http://localhost:3000', 'http://127.0.0.1:3000']
    
    # API配置
    API_VERSION = '1.0.0'
    API_PREFIX = '/api'
    
    # 分页配置
    DEFAULT_PAGE_SIZE = 20
    MAX_PAGE_SIZE = 100
    
    # 云存储配置
    CLOUD_STORAGE_PROVIDER = 'local'  # 默认使用本地存储
    CLOUD_STORAGE_CONFIG = {
        'tianyi': {
            'app_id': os.environ.get('TIANYI_APP_ID', ''),
            'app_secret': os.environ.get('TIANYI_APP_SECRET', ''),
            'access_token': os.environ.get('TIANYI_ACCESS_TOKEN', ''),
            'refresh_token': os.environ.get('TIANYI_REFRESH_TOKEN', '')
        },
        'local': {
            'storage_path': os.path.join(os.path.dirname(__file__), '..', 'uploads')
        }
    }
    
    # 文件上传配置
    MAX_CONTENT_LENGTH = 2 * 1024 * 1024 * 1024  # 2GB
    UPLOAD_CHUNK_SIZE = 8192
    ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'zip', 'rar', '7z', 'mp4', 'avi', 'mov', 'mp3', 'wav', 'flac'}
    
    # 用户配额配置
    DEFAULT_USER_QUOTA = 10 * 1024 * 1024 * 1024  # 10GB
    QUOTA_WARNING_THRESHOLD = 0.95  # 95%警告阈值

class DevelopmentConfig(Config):
    """开发环境配置"""
    DEBUG = True
    TESTING = False

class ProductionConfig(Config):
    """生产环境配置"""
    DEBUG = False
    TESTING = False

class TestingConfig(Config):
    """测试环境配置"""
    DEBUG = True
    TESTING = True
    SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:'

# 配置字典
config = {
    'development': DevelopmentConfig,
    'production': ProductionConfig,
    'testing': TestingConfig,
    'default': DevelopmentConfig
}